<template>
    <div>
        <!-- 查询条件开始 -->
        <el-form ref="queryForm" :model="queryParams" :inline="true" label-width="68px">
            <el-form-item label="厂家名称" prop="producerName">
                <el-input
                        v-model="queryParams.producerName"
                        placeholder="请输入厂家名称"
                        clearable
                        size="small"
                        style="width:240px"
                />
            </el-form-item>
            <el-form-item label="关键字" prop="keywords">
                <el-input
                        v-model="queryParams.keywords"
                        placeholder="请输入关键字"
                        clearable
                        size="small"
                        style="width:240px"
                />
            </el-form-item>
            <el-form-item label="手机号" prop="producerTel">
                <el-input
                        v-model="queryParams.producerTel"
                        placeholder="请输入手机号"
                        clearable
                        size="small"
                        style="width:240px"
                />
            </el-form-item>
            <el-form-item label="状态" prop="status">
                <el-select
                        v-model="queryParams.status"
                        placeholder="可用状态"
                        clearable
                        size="small"
                        style="width:240px"
                >
                    <el-option
                            v-for="dict in statusOptions"
                            :key="dict.dictValue"
                            :label="dict.dictLabel"
                            :value="dict.dictValue"
                    />
                </el-select>
            </el-form-item>
            <el-form-item label="创建时间">
                <el-date-picker
                        v-model="queryParams.createTime"
                        size="small"
                        style="width:240px"
                        value-format="yyyy-MM-dd"
                        type="daterange"
                        range-separator="-"
                        start-placeholde="开始日期"
                        end-placeholde="结束日期"
                />
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="resetQuePro">搜索</el-button>
                <el-button type="primary" icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>
        <!-- 表格工具按钮开始 -->
        <el-row :gutter="10" style="margin-bottom: 8px;">
            <el-col :span="1.5">
                <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAdd" >新增</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="danger" icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDeleteTop">删除</el-button>
            </el-col>
        </el-row>
        <!-- 表格工具按钮结束 -->
        <!-- 数据表格开始 -->
        <el-table  border :data="producerList" @selection-change="handleSelectionChnage" >
            <el-table-column type="selection" width="55" align="center" />
            <el-table-column label="厂家ID" align="center" prop="producerId" />
            <el-table-column label="厂家名称" align="center" prop="producerName" />
            <el-table-column label="厂家编码" align="center" prop="producerCode"/>
            <el-table-column label="联系人" align="center" prop="producerPerson" />
            <el-table-column label="电话" width="200" align="center" prop="producerTel"/>
            <el-table-column label="关键字" align="center" prop="keywords" />
            <el-table-column label="状态" prop="status" align="center" :formatter="statusFormat"/>
            <el-table-column label="创建时间" align="center" prop="createTime" width="200" />
            <el-table-column label="操作" align="center" width="250">
                <template slot-scope="scope">
                    <el-button type="text" icon="el-icon-edit" size="mini" @click="handleUpdate(scope.row)">修改</el-button>
                    <el-button type="text" icon="el-icon-delete" size="mini" @click="handleDelete(scope.row)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>
        <!-- 数据表格结束 -->
        <!-- 分页控件开始 -->
        <el-pagination
                v-show="total>0"
                :current-page="currentPage"
                :page-sizes="[5, 10, 20, 30]"
                :page-size="pageSize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total"
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
        />
        <!-- 分页控件结束 -->
        <!-- 添加修改弹出层开始 -->
        <el-dialog
                :title="title"
                :visible.sync="open"
                width="600px"
                center
                append-to-body
        >
            <el-form ref="form" :model="form" :rules="rules" label-width="120px">
                <el-form-item el-form-item label="厂家名称" prop="producerName">
                    <el-input v-model="form.producerName" placeholder="请输入厂家名称" clearable size="small" />
                </el-form-item>
                <el-form-item label="厂家编码" prop="producerCode">
                    <el-input v-model="form.producerCode" placeholder="请输入厂家编码" clearable size="small" />
                </el-form-item>
                <el-form-item label="联系人" prop="producerPerson">
                    <el-input v-model="form.producerPerson" placeholder="请输入联系人" clearable size="small" />
                </el-form-item>
                <el-form-item label="电话" prop="producerTel">
                    <el-input v-model="form.producerTel" placeholder="请输入电话" clearable size="small" />
                </el-form-item>
                <el-form-item label="关键字" prop="keywords">
                    <el-input v-model="form.keywords" placeholder="请输入关键字" clearable size="small" />
                </el-form-item>
                <el-form-item label="地址" prop="producerAddress">
                    <el-input v-model="form.producerAddress" placeholder="请输入地址" clearable size="small" />
                </el-form-item>
                <el-form-item label="状态" prop="status">
                    <el-radio-group v-model="form.status">
                        <el-radio
                                v-for="dict in statusOptions"
                                :key="dict.dictValue"
                                :label="dict.dictValue"
                                :value="dict.dictValue"
                        >{{ dict.dictLabel }}</el-radio>
                    </el-radio-group>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="handleSubmit('form')">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </span>
        </el-dialog>
        <!-- 添加修改弹出层结束 -->
    </div>
</template>

<script>
    import qs from 'qs'
    export default {
        data(){
            return{
                queryParams:{
                    producerName: undefined,
                    producerCode: undefined,
                    producerTel: undefined,
                    status: undefined,
                    keywords: undefined
                },
                // 选中数组
                ids: [],
                // 非单个禁用
                single: true,
                // 非多个禁用
                multiple: true,
                producerIds:[],
                //所有厂家信息
                prodOptions:[],
                //所有信息
                producerList:[],
                //所有状态
                statusOptions:[],

                createTime:[],
                //总条数
                total:0,
                pageSize:5,
                currentPage:1,
                // 弹出层标题
                title: '',
                // 是否显示弹出层
                open: false,
                // 表单数据
                form: {},
                // 表单校验
                rules: {
                    producerName:[
                        {required: true, message: '厂家名称不能为空', trigger: 'blur' }
                    ],
                    producerTel:[
                        { required: true, message: '联系电话不能为空', trigger: 'blur',validator: checkPhone }
                    ],
                    keywords:[
                        { required: true, message: '查询关键字不能为空', trigger: 'blur' }
                    ]
                }
            }
            var checkPhone = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('联系电话不可为空！'));
                } else {
                    let regPone = null;
                    let mobile = /^1[3|4|5|7|8|9][0-9]\d{8}$/; //最新16手机正则
                    let tel = /^([0-9]{3,4}-)?[0-9]{7,8}$/; //座机
                    if (value.charAt(0) == 0) {   // charAt查找第一个字符方法，用来判断输入的是座机还是手机号
                        regPone = tel;
                    } else {
                        regPone = mobile;
                    }
                    if (!regPone.test(value)) {
                        callback(new Error("请输入正确的电话格式！"))
                    }
                    callback();
                }
            }
        },
        created(){
          //加载所有厂家
          this.initProducer();
          //加载厂家状态
          this.initStatus();
        },
        methods:{
            // 打开添加的弹出层
            handleAdd() {
                this.open = true
                this.reset()
                this.title = '添加生产厂家信息'
            },
            // 执行删除
            handleDelete(row) {
                const producerIds = row.producerId || this.ids
                const tx = this
                tx.$confirm('此操作将永久删除该生产厂家数据, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    this.$axios.delete("/stock/prod/prod/deleteProdByIds/"+producerIds).then(res => {
                        tx.msgSuccess('删除成功')
                        tx.currentPage=1
                        tx.initProducer()// 全查询
                    }).catch(() => {
                        tx.msgError('删除失败')
                    })
                }).catch(() => {
                    tx.msgError('删除已取消')
                })
            },
            // 执行批量删除
            handleDeleteTop(row) {
                const tx = this
                tx.$confirm('此操作将永久删除该生产厂家数据, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    this.$axios.post("/stock/prod/prod/delAll/",tx.producerIds).then(res => {
                        tx.msgSuccess('删除成功')
                        tx.currentPage=1
                        tx.initProducer()// 全查询
                    }).catch(() => {
                        tx.msgError('删除失败')
                    })
                }).catch(() => {
                    tx.msgError('删除已取消')
                })
            },
            // 打开修改的弹出层
            handleUpdate(row) {
                this.title = '修改生产厂家信息'
                this.open = true
                this.reset()
                this.form=JSON.parse(JSON.stringify(row))
            },
            // 保存
            handleSubmit(form) {
                this.$refs['form'].validate((valid) => {
                    if (valid){
                        const tx = this
                        tx.$axios.post("/stock/prod/prod/addProd/",tx.form).then(res => {
                            if (tx.form.medicinesId === undefined) {
                                tx.msgSuccess('保存成功')
                                tx.initProducer()// 列表重新查询
                                tx.open = false// 关闭弹出层
                            } else { // 做修改
                                tx.msgSuccess('修改成功')
                                tx.initProducer()// 列表重新查询
                                tx.open = false// 关闭弹出层
                            }
                        })
                    }
                })
            },
            // 取消
            cancel() {
                this.open = false
                this.title = ''
            },
            initStatus(){
                this.$axios.get("/stock/prod/dict/data/findByType/sys_normal_disable").then(result=>{
                    this.statusOptions=result.data.data;
                })
            },
            //状态字典的解析
            statusFormat(row){
                let v=row.status;
                return this.formatDict(this.statusOptions,v)
            },
            //重置查询条件
            resetQuery(){
                this.resetForm('queryForm')
                this.queryParams.createTime=[]
                this.initProducer()
            },
            //搜索
            resetQuePro(){
                this.currentPage=1
                this.initProducer()
            },
            initProducer(){
                this.$axios.post("/stock/prod/prod/prodList/"+this.currentPage+"/"+this.pageSize,this.queryParams).then(result=>{
                    this.producerList=result.data.data.records;
                    this.total=result.data.data.total;
                })
            },
            // 分页pageSize变化时触发
            handleSizeChange(val) {
                this.pageSize = val
                // 重新查询
                this.initProducer()
            },
            // 点击上一页  下一页，跳转到哪一页面时触发
            handleCurrentChange(val) {
                this.currentPage = val
                // 重新查询
                this.initProducer()
            },
            // 数据表格的多选择框选择时触发
            handleSelectionChnage(selection) {
                this.ids = selection.map(item => item.producerId)
                this.single = selection.length !== 1
                this.multiple = !selection.length
                this.producerIds=selection
            },
            // 重置表单
            reset() {
                this.resetForm('form')
                this.form = {
                    producerId: undefined,
                    producerName: undefined,
                    producerCode: undefined,
                    producerTel: undefined,
                    keywords: undefined,
                    status: '0'
                }
            }
        }
    }
</script>

<style scoped>

</style>
